package com.lemon.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import com.lemon.common.core.constant.CacheConstants;
import com.lemon.common.core.constant.ConfigConstants;
import com.lemon.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.system.mapper.BiElementsMapper;
import com.lemon.system.domain.BiElements;
import com.lemon.system.service.IBiElementsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 包名称：com.lemon.system.service.impl
 * 类名称：BiElementsServiceImpl
 * 类描述：要素信息Service业务层处理
 * 创建人：@author huangpeng
 * 创建时间：2023-03-12
 */
@Service
public class BiElementsServiceImpl extends BaseServiceImpl<BiElements> implements IBiElementsService {

    @Autowired
    private BiElementsMapper biElementsMapper;


    @Autowired
    private RedisService redisService;

    @Override
    public List<BiElements> selectBiElementsList(BiElements biElements) {
        return biElementsMapper.selectBiElementsList(biElements);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveElements(List<BiElements> list) {
        int count = 0;
        if (CollUtil.isNotEmpty(list)) {
            String functionCode = list.get(0).getFunctionCode();
            String cacheKey = functionCode.toLowerCase() + CacheConstants.KEY_SUFFIX;
            // 先删除后增加
            biElementsMapper.deleteByFunctionCode(functionCode);
            Map<String, Object> elementMap = new HashMap<>();
            for (BiElements elements : list) {
                OptEntityUtils.preInsert(elements);
                elementMap.put(elements.getElementCode(), elements.getElementValue());
            }
            if (redisService.hasKey(cacheKey)) {
                redisService.deleteObject(cacheKey);
            }
            redisService.setCacheMap(cacheKey, elementMap);
            count = biElementsMapper.batchInsert(list);
        }
        return count;
    }

    @Override
    public Map<String, Object> getSiteConfig() {
        Map<String, Object> configMap = new HashMap<>();
        String cacheKey = ConfigConstants.SITE_CONFIG.toLowerCase() + CacheConstants.KEY_SUFFIX;
        if (redisService.hasKey(cacheKey)) {
            configMap = redisService.getCacheMap(cacheKey);
        } else {
            BiElements element = new BiElements();
            element.setFunctionCode(ConfigConstants.SITE_CONFIG);
            List<BiElements> elementsList = biElementsMapper.selectBiElementsList(element);
            for (BiElements elementInfo : elementsList) {
                configMap.put(elementInfo.getElementCode(), elementInfo.getElementValue());
            }
            redisService.setCacheMap(cacheKey, configMap);
        }
        return configMap;
    }

    @Override
    public String getElValueByElCodeAndFCode(String elementCode, String functionCode) {
        return biElementsMapper.selectElValueByElCodeAndFCode(elementCode, functionCode);
    }

}
